  import moment from 'moment'

  // 适配函数
  export const setFieldAdaptor = (data: BasicListApi.PageData) => {
    if (data?.dataSource && data?.layout?.tabs) {
      const result = {}
      data.layout.tabs.forEach(tab => {
        tab.data.forEach(field => {
          switch (field.type) {
            case 'datetime':
              result[field.key] = moment(data.dataSource[field.key])
              break;
            default:
              result[field.key] = data.dataSource[field.key]
              break;
          }
        })
      });
      return result
    }
    return {}

  }
  // 请求接口适配函数
  export const submitFieldsAdaptor = (formValues: any) => {
    const result = formValues;
    Object.keys(formValues).forEach((key) => {
      if (moment.isMoment(formValues[key])) {
        result[key] = moment(formValues[key]).format();
      }
      if (Array.isArray(formValues[key])) {
        result[key] = formValues[key].map((innerValue: any) => {
          if (moment.isMoment(innerValue)) {
            return moment(innerValue).format();
          }
          return innerValue;
        });
      }
    });
    return result;
  };

